package com.yt.service.impl;

import com.yt.dao.OrderDao;
import com.yt.entity.Order;
import com.yt.service.AccountService;
import com.yt.service.OrderService;
import com.yt.service.StorageSerivce;
import com.yt.util.CommonResult;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import io.seata.tm.api.GlobalTransactionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Classname OrderServiceImpl
 * @Description
 * @Date 2021/11/2 11:08
 * @Create by 杨涛
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Resource
    OrderDao orderDao;

    @Resource
    AccountService accountService;

    @Resource
    StorageSerivce storageSerivce;

    @GlobalTransactional(name="create-order",rollbackFor = Exception.class)
    @Override
    public void create(Order order) {

       String xid = GlobalTransactionContext.getCurrentOrCreate().getXid();
        RootContext.bind(xid);
        log.warn("-----添加订单-------");
        orderDao.create(order);

        log.warn("----扣库存----");
        storageSerivce.decrease(order.getProductId(),order.getCount(),xid);

        log.warn("-------扣余额------");
        accountService.decrease(order.getUserId(),order.getMoney(),xid);

        log.warn("-----修改订单状态-------");
        orderDao.update(order.getUserId(),1);
    }
}
